﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xClient.Framework;
using xClient.Framework.BaseView;
using xClient.Framework.PlugCore;

namespace cMES3001
{
    [Plug(ID = "cMES3001",
          Name = "WIP KPS数据调整",
          Description = "WIP KPS数据调整",
          Parent = "2,控制模块")]
    public partial class cMES3001 : StdView
    {
        public cMES3001()
        {
            InitializeComponent();
        }

        private void cMES3001_Load(object sender, EventArgs e)
        {

        }

        private void LoadWipKpsList()
        {
            btnSearch_Click(null, null);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtSearch.Text.Trim()))
            {
                ShowMessage($"查询的SN：{txtSearch.Text}不能为空", MsgType.NG);
            }
            else
            {
                ExecutionResult<dynamic> res = Services.ServiceMethod.GetWipKpsBySn(txtSearch.Text.Trim());

                if (!res.Status)
                {
                    ShowMessage(res.Message, MsgType.NG);
                }
                else
                {
                    if (res.Data.Count > 0)
                    {
                        datalistbom.DataSource = res.Data;
                        ShowMessage("查询成功", MsgType.OK);
                    }
                    else
                    {
                        datalistbom.Rows.Clear();
                        ShowMessage($"没有查到SN的KPS信息：{txtSearch.Text}", MsgType.NG);
                    }

                    datalistbom.ClearSelection();
                }
            }
        }

        private void Edit_Click(object sender, EventArgs e)
        {
            cEdit edit = default(cEdit);

            if (((ToolStripButton)sender).Tag.Equals("ADD"))
            {
                //edit = new cEdit("ADD", new { txtSearch.Text });

                //DialogResult result = edit.ShowDialog();

                //if (result == DialogResult.OK)
                //{
                //    this.ShowMessage("数据添加成功！", MsgType.OK);

                //    this.LoadBomList();
                //}

                //if (result == DialogResult.No)
                //{
                //    this.ShowMessage("数据添加失败！", MsgType.NG);
                //}
            }

            if (((ToolStripButton)sender).Tag.Equals("EDIT"))
            {
                if (datalistbom.SelectedRows.Count < 1)
                {
                    ShowMessage("请选择一个料号信息进行编辑.", MsgType.NG);
                    return;
                }

                object[] xx = new object[]
                {
                    txtSearch.Text,//sn
                    datalistbom.SelectedRows[0].Cells[0].Value, // kpn
                    datalistbom.SelectedRows[0].Cells[1].Value, // kps
                    datalistbom.SelectedRows[0].Cells[2].Value, //pickuptime
                    datalistbom.SelectedRows[0].Cells[3].Value,//qty
                    datalistbom.SelectedRows[0].Cells[4].Value,//alternative
                    datalistbom.SelectedRows[0].Cells[5].Value,//isvald
                    datalistbom.SelectedRows[0].Cells[6].Value,//remarks
                };

                edit = new cEdit("EDIT", new object[]
                {
                    txtSearch.Text,//sn
                    datalistbom.SelectedRows[0].Cells[0].Value, // kpn
                    datalistbom.SelectedRows[0].Cells[1].Value, // kps
                    datalistbom.SelectedRows[0].Cells[2].Value, //pickuptime
                    datalistbom.SelectedRows[0].Cells[3].Value,//qty
                    datalistbom.SelectedRows[0].Cells[4].Value,//alternative
                    datalistbom.SelectedRows[0].Cells[5].Value,//isvald
                    datalistbom.SelectedRows[0].Cells[6].Value,//remarks
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadWipKpsList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }

            if (((ToolStripButton)sender).Tag.Equals("DELETE"))
            {
                //if (datalistbom.SelectedRows.Count < 1)
                //{
                //    ShowMessage("请选择一个需要删除的Bom材料信息.", MsgType.NG);
                //    return;
                //}

                //DialogResult dialog = MessageBox.Show("确定要删除选中吗？", "系统消息", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                ////{datalistbom.SelectedRows[0].Cells[0].Value}
                //if (dialog == DialogResult.Yes)
                //{
                //    if (datalistbom.SelectedRows.Count >= 1)
                //    {
                //        //
                //        string vm = JsonConvert.SerializeObject(new
                //        {
                //            bomno = txtSearch.Text,  //bomno
                //            partno = datalistbom.SelectedRows[0].Cells[0].Value.ToString()  //keypartno
                //        });

                //        ExecutionResult result = Services.ServiceMethod.DeleteBomByBomPartNo(vm);

                //        if (result.Status && result.ExecuteCount > 0)
                //        {
                //            this.ShowMessage($"数据{datalistbom.SelectedRows[0].Cells[0].Value}删除成功！", MsgType.OK);

                //            this.LoadWipKpsList();
                //        }
                //    }
                //}
            }
        }

        private void datalistbom_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                cEdit edit = new cEdit("EDIT", new object[]
                {
                    txtSearch.Text,//sn
                    datalistbom.SelectedRows[0].Cells[2].Value, // kpn
                    datalistbom.SelectedRows[0].Cells[3].Value, // kps
                    datalistbom.SelectedRows[0].Cells[4].Value, //线体
                    datalistbom.SelectedRows[0].Cells[5].Value,//站别
                    datalistbom.SelectedRows[0].Cells[7].Value,// qty  
                    datalistbom.SelectedRows[0].Cells[6].Value,// 捡料时间  
                    datalistbom.SelectedRows[0].Cells[8].Value,//  alternative  
                    datalistbom.SelectedRows[0].Cells[9].Value,//  isvald
                    datalistbom.SelectedRows[0].Cells[10].Value,  //remarks
                });

                DialogResult result = edit.ShowDialog();

                if (result == DialogResult.OK)
                {
                    this.ShowMessage("数据修改成功！", MsgType.OK);

                    this.LoadWipKpsList();
                }

                if (result == DialogResult.No)
                {
                    this.ShowMessage("数据修改失败！", MsgType.NG);
                }
            }
        }
    }
}
